/** @file    NotificationFlow.cpp
 *  @author  Alessandro Polo
 *  @version $Id: NotificationFlow.cpp 2621 2010-06-04 12:23:48Z alex $
 *  @brief
 * File containing methods for the wosh::communication::NotificationFlow class.
 * The header for this class can be found in NotificationFlow.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/communication/NotificationFlow.h>

 #include <core/ObjectAllocator.h>
 #include <core/Mutex.h>
 #include <core/Utilities.h>


using namespace std;
namespace wosh {
 namespace communication {

 WOSH_REGISTER(wosh::communication::NotificationFlow, "wosh::ISerializable", 1.0, _static_NotificationFlow )

long NotificationFlow_ID = 0;
Mutex NotificationFlow_ID_mux;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

NotificationFlow::NotificationFlow( Notification* original_notification ) {
	this->original = NULL;
	clear();
	assign_ID();
	setOriginal(original_notification);
 }

NotificationFlow::NotificationFlow( const NotificationFlow& m ) {
	this->original = NULL;
	clear();
	*this = m;
 }

NotificationFlow::~NotificationFlow() {
	clear();
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////// CONFIGURATION

void NotificationFlow::assign_ID()
 {
	//this->uid = Utilities::randomLong();
	NotificationFlow_ID_mux.lock();
	this->uid = ++NotificationFlow_ID;
	NotificationFlow_ID_mux.unLock();
 }

void NotificationFlow::clear()
 {
	this->uid = 0;

	this->archived = false;

	this->sentCounter = 0;
	this->sentTs= 0;

	this->processingTs = 0;

	this->sourceCommunicator.clear();
	this->senderCommunicator.clear();

	if ( this->original != NULL )
		delete this->original;
	this->original = NULL;

	std::vector<Notification*>::iterator it;
	for ( it=this->interpreted.begin(); it!=this->interpreted.end(); ++it ) {
		if ( *it == NULL ) continue;
		delete *it;
	 }
	this->interpreted.clear();

	this->processed = 0;
	this->errors = 0;
	this->errorTs = 0;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void NotificationFlow::setOriginal( Notification* notification )
 {
	if ( this->original != NULL )
		delete this->original; this->original = NULL;
	this->original = notification;
	if ( this->original != NULL )
		this->original->setFlowID( this->uid );
 }

void NotificationFlow::addInterpreted( Notification* interpreted_notification )
 {
	if ( interpreted_notification== NULL ) return;
	this->interpreted.push_back(interpreted_notification);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void NotificationFlow::setProcessing( bool value, long epoch )
 {
	if ( value ) {
		++this->processed;
		if ( epoch == 0 )
			this->processingTs = Utilities::std_time();
		else
			this->processingTs = epoch;
	 }
	else
		this->processingTs = 0;
 }

void NotificationFlow::raiseError( int offset )
 {
	if ( offset == 0 ) {
		this->errors = 0;
		this->errorTs = 0;
	 }
	else {
		this->errors += offset;
		this->errorTs = Utilities::std_time();
	 }
 }

void NotificationFlow::sending( int offset )
 {
	if ( offset == 0 ) {
		this->sentCounter = 0;
		this->sentTs = 0;
	 }
	else {
		this->sentCounter += offset;
		this->sentTs = Utilities::std_time();
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

const Notification* NotificationFlow::getFinal() const
 {
	if ( this->interpreted.empty() )
		return this->original;
	return this->interpreted.back();
 }

Notification* NotificationFlow::getFinal()
 {
	if ( this->interpreted.empty() )
		return this->original;
	return this->interpreted.back();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

NotificationFlow& NotificationFlow::operator=( const NotificationFlow& m )
 {
	if (this == &m) return *this; // same object?

	this->uid = m.uid;
	this->archived = m.archived;
	this->sentCounter = m.sentCounter;
	this->sentTs = m.sentTs;
	this->processingTs = m.processingTs;
	this->processed = m.processed;

	this->errors = m.errors;
	this->errorTs = m.errorTs;

	this->sourceCommunicator = m.sourceCommunicator;
	this->senderCommunicator = m.senderCommunicator;

	if ( this->original != NULL )
		delete this->original;
	this->original = NULL;
	if ( m.original != NULL )
		this->original = m.original->clone();


	return *this;
 }

bool NotificationFlow::operator==( const NotificationFlow &other ) const
 {
	if ( this->archived != other.archived )
		return false;

	if ( this->sentCounter != other.sentCounter )
		return false;

	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////// DEBUG & INFO
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////// SERIALIZATION

std::string NotificationFlow::toString() const
 {
	return "";
 }

bool NotificationFlow::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ret = hash_context->update( this->uid ) & ret;
	ret = hash_context->update( this->archived ) & ret;
	ret = hash_context->update( this->errors ) & ret;
	ret = hash_context->update( this->errorTs ) & ret;
	ret = hash_context->update( this->sentCounter ) & ret;
	ret = hash_context->update( this->sentTs ) & ret;
	ret = hash_context->update( this->processingTs ) & ret;
	ret = hash_context->update( this->processed ) & ret;
	if ( this->original != NULL )
		ret = this->original->updateHash(hash_context) & ret;
	std::vector<Notification*>::const_iterator it;
	for ( it=this->interpreted.begin(); it!=this->interpreted.end(); ++it ) {
		if ( *it == NULL ) continue;
		ret = (*it)->updateHash(hash_context) & ret;
	 }
	ret = this->sourceCommunicator.updateHash(hash_context) & ret;
	ret = this->senderCommunicator.updateHash(hash_context) & ret;
	return ret;
 }

/////////////////////////////////////////////////////////////////////////////////////////////// SERIALIZATION
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace communication
}; // namespace wosh
